{% set form = form_function %}
{% set each_device = each_function %}
{% set dict_options = dict_controllers[each_function.device] %}
{% set unique_id = each_function.unique_id %}
{% set controller_type = "Function" %}

<div id="mod_function_{{each_function.unique_id}}">
  <form id="mod_function_form" method="post" action="/function">
    {{form_conditional.csrf_token}}
    {{form_conditional.function_id(value=each_function.unique_id)}}
    {{form_conditional.function_type(value='function')}}

    <div class="row small-gutters" style="padding: 0.5em;">
      <div class="col-auto">
        <button type="button" class="form-control btn btn-primary btn-block" data-dismiss="modal">{{_('Close')}}</button>
      </div>
      <div class="col-auto small-gutters">
        <input onclick="return $(this).processRequest(this, 'function_mod');" name="function_mod" value="{{_('Save')}}" class="form-control btn btn-primary btn-block" type="button"/>
      </div>
      <div class="col-auto small-gutters">
        <input onclick="return confirm('{{_('Are you sure you want to delete this?')}}') && $(this).processRequest(this, 'function_delete');" name="function_delete" value="{{_('Delete')}}" class="form-control btn btn-primary btn-block" type="button"/>
      </div>
    </div>

    {% if each_function.device in dict_controllers %}

    <div class="row small-gutters" style="padding: 0.5em">

      {% if 'function_manufacturer' in dict_controllers[each_function.device] %}
      <div class="col-12">
        {{_("Manufacturer")}}: {{dict_controllers[each_function.device]['function_manufacturer']}}
      </div>
      {% endif %}

      {% if 'function_name' in dict_controllers[each_function.device] %}
      <div class="col-12">
        {{_("Name")}}: {{dict_controllers[each_function.device]['function_name']}}
      </div>
      {% endif %}

      {% if 'measurements_name' in dict_controllers[each_function.device] %}
      <div class="col-12">
        {{_("Measurements")}}: {{dict_controllers[each_function.device]['measurements_name']}}
      </div>
      {% endif %}

      {% if 'dependencies_module' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['dependencies_module'] %}
        <div class="col-12">
        {{_("Library")}}:
        {% for each_lib in dict_controllers[each_function.device]['dependencies_module'] -%}
          {% if each_lib[0] == 'pip-pypi' %}
            {% if each_lib[2].startswith("git+") %}
          {{each_lib[2]}}
            {% elif "==" in each_lib[2] %}
          <a href="https://pypi.org/project/{{each_lib[2].split('==')[0]}}/{{each_lib[2].split('==')[1]}}" target="_blank">{{each_lib[2].split('==')[0]}}</a>
            {% else %}
          <a href="https://pypi.org/project/{{each_lib[2]}}" target="_blank">{{each_lib[2]}}</a>
            {%  endif %}
          {% elif each_lib[0] == 'internal' %}
            {{each_lib[2]}}
          {% else %}
            {{each_lib[1]}}
          {% endif %}
          {%- if not loop.last %}, {% endif %}
        {% endfor %}
        </div>
      {% endif %}

      {% set manufacturer_url = 'url_manufacturer' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['url_manufacturer'] %}
      {% set datasheet_url = 'url_datasheet' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['url_datasheet'] %}
      {% set product_purchase_url = 'url_product_purchase' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['url_product_purchase'] %}
      {% set additional_url = 'url_additional' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['url_additional'] %}

      {% if manufacturer_url or datasheet_url or product_purchase_url or additional_url %}
      <div class="col-12">
        {{_("Literature") + ": "}}
        {%- if manufacturer_url -%}
        {{_("Manufacturer")}} {% for each_url in dict_controllers[each_function.device]['url_manufacturer'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
        {%- endif -%}
        {%- if datasheet_url -%}
        {% if manufacturer_url %}; {% endif -%}
        {{_("Datasheet")}} {% for each_url in dict_controllers[each_function.device]['url_datasheet'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
        {%- endif -%}
        {%- if product_purchase_url -%}
        {% if manufacturer_url or datasheet_url %}; {% endif -%}
        {{_("Product")}} {% for each_url in dict_controllers[each_function.device]['url_product_purchase'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
        {%- endif -%}
        {%- if additional_url -%}
        {% if manufacturer_url or datasheet_url or product_purchase_url %}; {% endif -%}
        {{_("Additional")}} {% for each_url in dict_controllers[each_function.device]['url_additional'] %}<a href="{{each_url}}" target="_blank">Link {{loop.index}}</a>{{ ", " if not loop.last }}{% endfor %}
        {%- endif -%}.
      </div>
      {% endif %}

      {% if 'message' in dict_controllers[each_function.device] %}
      <div class="col-12" style="padding-top: 0.3em">
        {{dict_controllers[each_function.device]['message']|safe}}
      </div>
      {% endif %}

    </div>

    {% if 'options_enabled' in dict_controllers[each_function.device] and 'function_status' in dict_controllers[each_function.device]['options_enabled'] %}
    <div id="status_activated_enable_{{unique_id}}" class="row small-gutters align-items-end" style="{% if not each_function.is_activated %}display: none; {% endif %}padding: 0.5em">
      <div class="col-12">
        <h5>{{_('Activated Status')}}</h5>
      </div>
      <div class="col-12">
        <span id="function_status_activated_{{unique_id}}"></span>
      </div>
    </div>
    {% endif %}

    <!-- HTML for Function option function_status -->
    {% if 'function_status' in dict_controllers[each_function.device] and dict_controllers[each_function.device]['function_status'] %}
    <div id="function_status_enable_{{unique_id}}" class="row small-gutters align-items-end" style="padding: 0.5em">
      <div class="col-12">
        <h5>{{_('Status')}}</h5>
      </div>
      <div class="col-12">
        <span id="function_status_always{{unique_id}}"></span>
      </div>
    </div>
    {% endif %}

    <div id="return_text_{{unique_id}}" class="col-12"></div>

    {% if each_function.device in custom_commands %}
    <div class="row small-gutters align-items-end" style="padding: 0.5em">
      <div class="col-12" style="padding-top: 1em"></div>
      <div class="col-12">
        <h5>{{_('Commands')}}</h5>
        <hr class="entry-break">
      </div>
      {% include 'pages/form_options/Custom_Actions_Message.html' %}
      {% if each_function.device in custom_commands and custom_commands[each_function.device] %}
        {% set force_default_value = true %}
        {% set custom_options_values = none %}
        {% if 'custom_commands' in dict_options %}
          {% for each_option in dict_options['custom_commands'] %}
            {% include 'pages/form_options/Custom_Options.html' %}
          {% endfor %}
        {% endif %}
      {% endif %}
    </div>
   {% endif %}

    <div class="row small-gutters" style="padding: 0.5em">
      <div class="col-12">
        <h5>{{_('Options')}}</h5>
        <hr class="entry-break">
      </div>
      <div class="col-auto">
        {{form_function.name.label(class_='control-label')}}
        <div>
          {{form_function.name(class_='form-control', value=each_function.name, **{'title': dict_translation['name']['phrase']})}}
        </div>
      </div>
    {% if each_function.device != 'function_spacer' %}
      <div class="col-auto">
        {{form_function.log_level_debug.label(class_='control-label')}}
        <div class="input-group-text">
         <input id="log_level_debug" name="log_level_debug" type="checkbox" title="{{dict_translation['log_level_debug']['phrase']}}" value="y"{% if each_function.log_level_debug %} checked{% endif %}>
        </div>
      </div>
    {% endif %}

    {% include 'pages/form_options/Num_channels.html' %}
    {% include 'pages/form_options/Measurements_Enabled.html' %}

    {% set custom_options_values = custom_options_values_controllers %}

    {% if 'custom_options' in dict_options and custom_options_values[unique_id] %}
      {% set force_default_value = false %}
      {% for each_option in dict_options['custom_options'] %}
        {% include 'pages/form_options/Custom_Options.html' %}
      {% endfor %}
    {% endif %}

    </div>

    {% endif %}

      {%- set function_channel_exists = [] -%}
      {% for each_function_channel in function_channel if each_function_channel.function_id == unique_id %}
        {%- do function_channel_exists.append(1) -%}
      {% endfor %}

      {% if function_channel_exists %}

    <div class="row small-gutters" style="padding: 1em 0 0 0.5em">
      <div class="col-12">
        <h5>{{_('Channel Options')}}</h5>
        <hr class="entry-break">
      </div>
    </div>

      {% endif %}

      {% set custom_options_values = custom_options_values_function_channels %}
      {% set force_default_value = true %}
      {% set channel_options = true %}

      {% for each_function_channel in function_channel if each_function_channel.function_id == unique_id %}

        {% set channel = each_function_channel.channel %}
        {% set name_prefix = each_device.unique_id + "_" + channel|string + "_" %}

    <hr/>
    <div class="row">
      <div class="col-auto">
        Channel {{each_function_channel.channel}}

        {%- if 'name' in custom_options_values[unique_id][each_function_channel.channel] and
             custom_options_values[unique_id][each_function_channel.channel]['name'] -%}
          {{': ' + custom_options_values[unique_id][each_function_channel.channel]['name']}}
        {%- elif 'channels_dict' in dict_controllers[each_function.device] and
                 'name' in dict_controllers[each_function.device]['channels_dict'][each_function_channel.channel] -%}
          {{': ' + dict_controllers[each_function.device]['channels_dict'][each_function_channel.channel]['name']}}
        {%- endif -%}
      </div>
    </div>

        {% if custom_options_values[unique_id] %}

          {% if 'custom_channel_options_message' in dict_options %}
    <div class="row small-gutters" style="padding: 1em">
        <div class="col-12" style="padding-bottom: 0.5em">
          {{dict_options['custom_channel_options_message']|safe}}
        </div>
    </div>
          {% endif %}

          {% if 'custom_channel_options' in dict_options %}
    <div class="row small-gutters" style="padding: 1em">
            {% for each_option in dict_options['custom_channel_options'] %}
              {% include 'pages/form_options/Custom_Options.html' %}
            {% endfor %}
    </div>
          {% endif %}
        {% endif %}

      {% endfor %}

    {% if ("options_disabled" in dict_options and
           ('measurements_select' not in dict_options['options_disabled'] and
            'measurements_configure' not in dict_options['options_disabled']))
            or
            "options_disabled" not in dict_options %}
      {% set show_measurement_settings = true %}
    {% endif %}

    {% if show_measurement_settings %}

    <div class="row small-gutters" style="padding: 0.5em 0.5em 0 0.5em">
      <div class="col-12">
        <h5>{{_('Measurement Settings')}}</h5>
        <hr class="entry-break">
      </div>
    </div>

    {% if "measurements_variable_amount" in dict_options and dict_options['measurements_variable_amount'] %}
      {% include 'pages/form_options/Measurements_Select.html' %}
    {% endif %}

    {% include 'pages/form_options/Measurements_Configure.html' %}

    {% endif %}

  </form>

  {% if 'enable_actions' in dict_options['options_enabled']%}

  <div class="row align-items-end small-gutters" style="border-top: 1px solid #ddd; padding-top: 0.5em">
    <h5>{{_('Actions')}} <a href="https://kizniche.github.io/Mycodo/Actions" target="_blank"><span style="font-size: 16px" class="fas fa-question-circle"></span></a></h5>
  </div>

  <form method="post" action="/function">
    {{form_actions.csrf_token}}
    {{form_actions.device_id(class_='form-control', value=each_function.unique_id)}}
    {{form_actions.function_type(class_='form-control', value='function')}}

    <div class="row align-items-end small-gutters" style="border-bottom: 1px solid #ddd; padding: 1.5em 0 0.2em 0.1em">
      <div class="col-auto">
        <select class="selectpicker" data-style="btn btn-primary" data-live-search="true" title="{{_('Action')}}: {{dict_translation['select_one']['title']}}" id="action_type_{{each_function.unique_id}}" name="action_type">
        {% for value, name in choices_actions %}
          <option value="{{value}}">{{name}}</option>
        {% endfor %}
        </select>
      </div>
      <div class="col-auto">
        <input onclick="return $(this).processRequest(this, 'add_action');" name="add_action" value="{{_('Add')}}" class="form-control btn btn-primary btn-block" type="button"/>
      </div>
    </div>

  </form>

  {% set channel_options = false %}
  {% set post_action = "/function" %}

  <div id="mod_function_actions_{{each_function.unique_id}}">
  {% for each_action in action if each_action.function_id == each_function.unique_id %}
    {% include 'pages/actions.html' %}
  {% endfor %}
  </div>

  {% endif %}

</div>
